package com.whnk.model.controller;

import com.alibaba.fastjson.JSONObject;
import com.whnk.common.core.web.controller.BaseController;
import com.whnk.common.core.web.domain.AjaxResult;
import com.whnk.common.core.web.page.TableDataInfo;
import com.whnk.common.log.annotation.Log;
import com.whnk.common.log.enums.BusinessType;
import com.whnk.common.security.annotation.PreAuthorize;
import com.whnk.model.domain.DefTTaskOpLog;
import com.whnk.model.domain.DefTTaskOpLogDetail;
import com.whnk.model.service.IDefTTaskOpLogDetailService;
import com.whnk.model.service.IDefTTaskOpLogService;
import com.whnk.model.vo.ActivitiHighLineDTO;
import com.whnk.model.vo.DefTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author Tong
 * @create 2021-10-20 15:01
 */
@RestController
@RequestMapping("/task")
public class DefTaskController extends BaseController {

    @Autowired
    private IDefTTaskOpLogService defTTaskOpLogService;

    @Autowired
    private IDefTTaskOpLogDetailService defTTaskOpLogDetailService;
    /**
     * 录入数据接口
     */
//    @PreAuthorize(hasPermi = "model:log:add")
//    @Log(title = "新增任务主记录表", businessType = BusinessType.INSERT)
    @PostMapping("/{code}")
    public AjaxResult add(@RequestBody DefTaskVo defTaskVo, @PathVariable String code)
    {
        DefTTaskOpLog defTTaskOpLog = new DefTTaskOpLog();
        defTTaskOpLog.setWorkflowCode(code);
        defTTaskOpLog.setPcsExmpId(defTaskVo.getPcsExmpId());
        defTTaskOpLog.setTaskType(defTaskVo.getTaskType());
        defTTaskOpLog.setTaskName(defTaskVo.getTaskName());
        defTTaskOpLog.setStartTime(defTaskVo.getStartTime());
        defTTaskOpLog.setTaskStatus(defTaskVo.getTaskStatus());
        defTTaskOpLog.setAssignBrcode(defTaskVo.getAssignBrcode());
        defTTaskOpLog.setOprNo(defTaskVo.getOprNo());
        defTTaskOpLog.setEndTime(defTaskVo.getEndTime());
        DefTTaskOpLog lt=  defTTaskOpLogService.selectDefTTaskOpLogByPcsExmpId(defTaskVo.getPcsExmpId());
        if(lt == null){
            defTTaskOpLogService.insertDefTTaskOpLog(defTTaskOpLog);
        }else {
            defTTaskOpLogService.updateDefTTaskOpLog(defTTaskOpLog);
        }
        DefTTaskOpLogDetail[] dtold = defTaskVo.getDefTTaskOpLogDetail();
        for(int i=0;i<dtold.length;i++){
            dtold[i].setWorkflowCode(code);
            defTTaskOpLogDetailService.insertDefTTaskOpLogDetail(dtold[i]);
        }
        return AjaxResult.success();
    }


    /**
     * 流程图高亮
     * @tonghui
     */
    @GetMapping("/gethighLine")
    public AjaxResult gethighLine(@RequestParam("pcsExmpId") String pcsExmpId) {
        List<DefTTaskOpLogDetail> list = defTTaskOpLogDetailService.selectDefTTaskOpLogDetailByPcsExmpId(pcsExmpId);
        //高亮节点ID
        Set<String> highPoint = new HashSet<>();
        list.forEach(s -> highPoint.add(s.getTaskType()));
        ActivitiHighLineDTO activitiHighLineDTO = new ActivitiHighLineDTO();
        activitiHighLineDTO.setHighPoint(highPoint);
        return AjaxResult.success(activitiHighLineDTO);
    }

    /**
     * 获取流程明细
     */
    @GetMapping("/getTheDetail")
    public TableDataInfo getTheDetail(@RequestParam("pcsExmpId") String pcsExmpId) {
        List<DefTTaskOpLogDetail> list = defTTaskOpLogDetailService.selectDefTTaskOpLogDetailByPcsExmpId(pcsExmpId);
        return getDataTable(list);
    }


}
